#!/usr/bin/env python
# -*- coding: utf-8 -*-


import wx
from ui.panel import Panel
from core import Core

import os
from core.ui.centralpanel import CentralPanel
from core.ide.langsmanager import LangsManager
from core.ide.datamangager import DataMangager
from core.ide.commandmanager import CommandManager
wildcard = "All files (*.*)|*.*|Python (*.py, *.pyw)|*.py; *.pyw"

class Extend:
    
    def __init__(self):
        self.id="EDITOR"
        self.name="Editor"
        
    
    def enable(self):
        DataMangager().getData("Editor","Start Page","http://google.ca")
        
        Core().getMenu().quickMenu("File", 0, "New", "Blank Document").setCommand("new")
        Core().getMenu().quickMenu("File", 0, "New", "Blank Document").setKeyListner("Ctrl+N")
        Core().getMenu().quickMenu("File", "New","|Suggest")
        Core().getMenu().quickMenu("File", "New","|Suggest","Python")
        Core().getMenu().quickMenu("File", "New","|Other")
        Core().getMenu().quickMenu("File", "New","|Other","Template")
        
        Core().getMenu().quickMenu("File", 1, "Open").setCommand('select_file')
        Core().getMenu().quickMenu("File", 1, "Open").setKeyListner("Ctrl+O")
        
        Core().getMenu().quickMenu("File", 2, "|Save", "Save").setCommand('save')
        Core().getMenu().quickMenu("File","|Save", "Save").setKeyListner("Ctrl+S")
        
        Core().getMenu().quickMenu("File", "|Save", "Save All").setCommand('saveAll')
        Core().getMenu().quickMenu("File", "|Save", "Save As").setCommand('saveas')
        Core().getMenu().quickMenu("File", "|Save", "Save As").setKeyListner("Ctrl+Shift+S")
        
        
        Core().getMenu().quickMenu("Edit","Cut")
        Core().getMenu().quickMenu("Edit","Copy")
        Core().getMenu().quickMenu("Edit","Past")
        Core().getMenu().quickMenu("Edit","|Search","Find").setCommand('find')
        Core().getMenu().quickMenu("Edit","|Search","Find").setKeyListner("Ctrl+F")
        
        
        
        CommandManager().listener("select_file",self.selectFile)
        CommandManager().listener("open",self.openFile)
        CommandManager().listener("save",self.saveFile)
        CommandManager().listener("find",self.find)
        #self.panel.setIteam(self.panel,0,0,True)
        #temp
        #self.openFile('../_project/python/type_file/main.py')
        
    def disable(self):
        pass
    
    def launch(self):
        pass
    
    def find(self):
        p=CentralPanel().getCurrentPage()
        if self.panel==p:
            p.toggleSearch()
    
    
    
    def saveFile(self):
        p = CentralPanel().getCurrentPage()
        p.save()
            
    def openFile(self,file_=None):
        ind=self.getFileIndex(file_)
        if ind is not None:
            CentralPanel().selectPage(ind)
            return
        self.panel = Panel(Core().getCentralPanel())
        self.panel.openFile(file_)
        CentralPanel().addPage(self.panel)
    
    def getFileIndex(self,file_):
        length= CentralPanel().getPageCount()
        for index in range(0,length):
            if CentralPanel().getPagetAt(index).data == file_:
                return index
        #for page in CentralPanel().getPages():
        #    if CentralPanel().getPagetAt(index).data == file_:
        #        return page
        return None
     
        
    def selectFile(self):
        dlg = wx.FileDialog(
            Core().getCentralPanel(), message="Open file",
            defaultDir=os.getcwd(), 
            defaultFile="",
            wildcard=wildcard,
            style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR
            )

        if dlg.ShowModal() == wx.ID_OK:
            paths = dlg.GetPaths()
            CommandManager().execute("open", paths[0]);
            #self.panel=Panel(Core().getCentralPanel())
            #self.panel.openFile(paths[0])
            #self.panel.setText(open(paths[0],'r').read(),LangsManager().getLangFromFile(paths[0]))

        dlg.Destroy()
        

    